package com.example.administrator.myjdlc;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/16.
 */

public class AppApplication extends Application {
    /**
     * 当前的Activity
     */
    private static Activity topActivity;
    public static AppApplication instance;
    /**
     * 存放的Activity的集合
     */
    private List<Activity> mList = new LinkedList<Activity>();
    @Override
    public void onCreate() {
        super.onCreate();
        initDate();
        initService();
        instance=this;
    }

    private void initDate() {
        instance = this;
    }

    public static Context getContext() {
        return instance;
    }

    /**
     *
     * @Title: initService
     * @Description:初始化服务及监听
     * @param:
     * @return: void
     * @throws
     */
    @SuppressLint("NewApi")
    private void initService() {
        // 注册Activity生命周期监听
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {

            public void onActivityStopped(Activity activity) {
            }

            public void onActivityStarted(Activity activity) {
            }

            public void onActivitySaveInstanceState(Activity activity,
                                                    Bundle outState) {
            }

            @SuppressLint("DefaultLocale")
            public void onActivityResumed(Activity activity) {
                // 给当前Activity赋值
                topActivity = activity;
                // 将Activity放入到Activity集合
                mList.add(activity);
            }

            public void onActivityPaused(Activity activity) {
            }

            public void onActivityDestroyed(Activity activity) {
            }

            public void onActivityCreated(Activity activity,
                                          Bundle savedInstanceState) {
                topActivity = activity;
            }
        });
    }
    /**
     * 获取当前的Activity
     *
     * @return
     */
    public static Activity getTopActivity() {
        return topActivity;
    }
    /**
     * 关闭所有的activity
     */
    public void exit() {
        try {
            for (Activity activity : mList) {
                if (activity != null)
                    activity.finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            mNotificationManager.cancelAll();
        }
    }
    /**
     * 移除某个activity
     *
     * @param name
     *            activity的名称
     */
    public void removeActivity(String name) {
        try {
            for (Activity activity : mList) {
                if (activity != null)
                    if (activity.getLocalClassName().contains(name)) {
                        activity.finish();
                    }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取某个activity
     *
     * @param name
     *            activity的名称
     */
    public Activity getActivity(String name) {
        Activity act = null;
        try {
            for (Activity activity : mList) {
                if (activity != null)
                    if (activity.getLocalClassName().contains(name)) {
                        act = activity;
                        break;
                    }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return act;
    }
}
